package t;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.StringWriter;

import java.sql.SQLException;
import java.util.*;

import org.apache.velocity.app.VelocityEngine;

import org.apache.velocity.Template;

import org.apache.velocity.VelocityContext;

import com.winskysoft.database.DatabaseManager;
import com.winskysoft.entity.Column;
import com.winskysoft.entity.Table;

/**
 * 
 * @author Liang.xf 2004-12-14
 */

public class HelloVelocity {

	public static void main(String[] args) throws Exception {

		String url = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8";
		String username = "root";
		String password = "root";
		try {
			DatabaseManager databaseManager = DatabaseManager.getDatabaseManager(url, username, password);
			List<String[]> set = databaseManager.queryList("show tables;");
			for (String[] s : set) {
				Table table = new Table();
				table.setTable(s[0]);
				List<Column> columnList = new ArrayList<Column>();
				table.setColumn(columnList);
				List<String[]> list = databaseManager.queryList("describe "
						+ s[0]);
				for (String[] x : list) {
					Column column = new Column();
					column.setColumn(x[0]);
					column.setType(x[1]);
					columnList.add(column);
					// for(String str:x){
					// System.out.print(str+"   ");
					// }
					// System.out.println();
				}
				// System.out.println();

				// ��ʼ����ȡ��Velocity����

				VelocityEngine ve = new VelocityEngine();

				ve.init();

				// ȡ��velocity��ģ��

				Template t = ve.getTemplate("hellovelocity.vm", "UTF-8");

				// ȡ��velocity��������context

				VelocityContext context = new VelocityContext();

				// ���������������

				context.put("table", table);

				// �����

				StringWriter writer = new StringWriter();
				
				
				String path=HelloVelocity.class.getResource("/").getPath();

				File file = new File(path+"/"+table.getTable()+".java");
				
				System.out.println(file.getAbsolutePath());
				FileWriter fileWriter=new FileWriter(file);

				// ת�����

				t.merge(context, fileWriter);

				fileWriter.flush();
				fileWriter.close();

				// System.out.println(writer.toString());

				return;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}

	}
}